"""
Case Type   : 资源池化
Case Name   : gs_collector 收集日志信息指定多个主机
Create At   : 2024.01
Owner       : chen
Description :
    1、指定集群中多个主机收集信息
    2、指定多个主机含有不在集群中的主机
Expect      :
    1、收集成功
    2、收集失败
"""

import os
import unittest
import datetime
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.tmp_path = os.path.join(os.path.dirname(macro.PG_LOG_PATH))

    def test_shared_storage(self):
        current_time = datetime.datetime.now()
        start_time = current_time.strftime('%Y%m%d %H:%M')
        end_time = (current_time + datetime.timedelta(minutes=5)).strftime('%Y%m%d %H:%M')
        text = '-----step1: 指定集群中多个主机收集信息' \
            ' expect: 收集成功-----'
        self.log.info(text)

        self.log.info('-----主机名称-----')
        check_cmd = f'hostname'
        self.log.info(check_cmd)
        hostname1 = self.pri_node.sh(check_cmd).result()
        self.log.info(hostname1)

        self.log.info('-----备机名称-----')
        standby_node = Node('Standby1DbUser')
        self.log.info(check_cmd)
        hostname2 = standby_node.sh(check_cmd).result()
        self.log.info(hostname2)

        self.log.info('-----收集信息-----')
        cmd = f'source {macro.DB_ENV_PATH};' \
            f'gs_collector --begin-time="{start_time}" --end-time="{end_time}"' \
            f' -h {hostname1},{hostname2}'
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn(self.constant.GS_COLLECTOR_SUCCESS_MSG, res)
        
        self.log.info('-----查看结果-----')
        collector_cmd = f'source {macro.DB_ENV_PATH};' \
                        f'cd {self.tmp_path}; ls; ' \
                        f'tar -zxvf collector*.tar.gz;'
        self.log.info(collector_cmd)
        res = self.pri_node.sh(collector_cmd).result()
        self.log.info(res)
        self.assertIn('Summary.log', res)
        self.assertIn('Detail.log', res)
        self.assertIn(f'{hostname1}.tar.gz', res)
        self.assertIn(f'{hostname1}.tar.gz', res)

        text = '-----step2: 指定多个主机含有不在集群中的主机' \
            ' expect: 收集失败-----'
        self.log.info(text)
        self.log.info('-----收集信息-----')
        cmd = f'source {macro.DB_ENV_PATH};' \
            f'gs_collector --begin-time="{start_time}" --end-time="{end_time}"' \
            f' -h {hostname1},ctupopengauss0010'
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('The host name [ctupopengauss0010] is not in the cluster', res)

    def tearDown(self):
        self.log.info('-----step3:清理环境-----')
        rm_cmd = f'''cd {self.tmp_path}; rm -rf collector_*'''
        self.log.info(rm_cmd)
        res = self.pri_node.sh(rm_cmd).result()
        self.log.info(res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')